/**
 * Project oo_thread
 */

#include "../include/HttpuUp.h"
#include "../include/MySQLConnector.h"
#include "../include/TLVmsg.h"
#include <ctime>
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
#include <curl/curl.h>
#include <stdio.h>
#include <string.h>
using std::endl;
using std::cout;
struct HttpResponseData
{
    char *buff;
    size_t size;
};
enum TaskType{
    TASK_LOGIN_SECTION1 = 1, 
    TASK_LOGIN_SECTION1_RESP_OK,
    TASK_LOGIN_SECTION1_RESP_ERROR,
    TASK_LOGIN_SECTION2,
    TASK_LOGIN_SECTION2_RESP_OK,
    TASK_LOGIN_SECTION2_RESP_ERROR,
    TASK_TYPE_REGISTER1,
    TASK_TYPE_REGISTER1_RESP_OK,
    TASK_TYPE_REGISTER1_RESP_ERROR,
    TASK_TYPE_GEGISTER2,
    TASK_TYPE_REGISTER2_RESP_OK,
    TASK_TYPE_HTTP_UP,
    TASK_TYPE_HTTP_UP_RESP_OK
};   

/**
 * RegisterTask implementation
 */
HttpUp::HttpUp(const string msg, TcpConnectionPtr conn)
:_msg(msg)
,_conn(conn)
{

}

/**
 * @return void
 */
//回调函数
size_t write_backfunc(void *contents, size_t size, size_t nmemb, void *userp)
{
    cout << "in write backfunc" << endl;
    struct HttpResponseData* data = (struct HttpResponseData *)userp;
    size_t realsize = nmemb * size;
    cout << "realsize :" << realsize << endl;
    if(!data) return 0;
    if(!data->buff)
    {
        cout << "data is null" << endl;
        data->buff = (char *)malloc(realsize + 1);
        data->size = 0;
    }
    else
    {
        cout << "data is not null" << endl;
        //有数据,先分配空间
        data->buff = (char *)realloc(data->buff, data->size + realsize + 1 );
    }
    if(!data->buff) return 0;
    cout << "bufore memcpy" << endl;
    memcpy(data->buff + data->size, contents, realsize);
    cout << "after memcpy" << endl;
    data->size += realsize;
    data->buff[data->size] = '\0';
    return realsize;
}
void HttpUp::process() {

    //handle msg
    cout << "HttpUp handle msg" << endl;
    // string sql = "select * from user where name = '" + _msg + "'";
    //
    // _conn->sendtoLoop(dwTASK_TYPE_REGISTER1_RESP_OK
    cout << "httpmsg = " << _msg << endl;
    CURL *curl;
    struct HttpResponseData response = {NULL, 0};
    CURLcode res;
    //初始化libcurl
    curl = curl_easy_init();
    if(curl)
    {
        const char *url = _msg.c_str();
        curl_easy_setopt(curl,CURLOPT_URL, url);
        //设置回调函数
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_backfunc);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);  // 10秒超时
        //执行请求
        res = curl_easy_perform(curl);
         // 检查执行结果
        if(res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));
        } else {
            // 打印响应
            printf("Response:%s\n", response.buff);
        }
        string resp = string(response.buff);
        //返回给客户端
        _conn->sendtoLoop(TASK_TYPE_HTTP_UP_RESP_OK,resp);
        // 清理资源
        curl_easy_cleanup(curl);
        free (response.buff);
    }


}
